package cn.mw.cmdb.viewProcessor;

import cn.mw.cmdb.entity.InstanceInfo;
import cn.mw.cmdb.processor.TableDataProcessorContext;
import cn.mw.cmdb.view.ViewConvertContext;
import cn.mw.microMonitorCommon.api.common.MwEngineManageService;
import cn.mw.microMonitorCommon.api.common.MwTPServerService;
import cn.mw.microMonitorCommon.entity.cmdb.RelatedDataType;
import cn.mw.microMonitorCommon.entity.cmdb.TableHeaderItem;
import cn.mw.microMonitorCommon.entity.common.MwEngineManageTableDto;
import cn.mw.microMonitorCommon.entity.common.MwEngineProxyGroupInfo;
import cn.mw.microMonitorCommon.entity.common.MwTPServerTable;
import com.google.common.base.Strings;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.mw.cmdb.processor.MonitorValue.*;
import static cn.mw.cmdb.util.ValConvertUtil.intValueConvert;
import static cn.mw.cmdb.util.ValConvertUtil.strValueConvert;

/**
 * 监控信息转换
 */
public class MonitorServerInfoProcessor implements RelateDataViewProcessor<InstanceInfo, Map> {
    private ViewProcessorManage viewProcessorManage;
    private static final String proxyGroupKey = "mwProxyGroup";
    private static final String monitorServerNameKey = "monitorServerName";
    private static final String pollingEngineNameKey = "pollingEngineName";
    private List<TableHeaderItem> tableHeaderItems;

    public MonitorServerInfoProcessor(ViewProcessorManage viewProcessorManage) {
        this.viewProcessorManage = viewProcessorManage;
    }

    public MonitorServerInfoProcessor(ViewProcessorManage viewProcessorManage, List<TableHeaderItem> tableHeaderItems) {
        this.viewProcessorManage = viewProcessorManage;
        this.tableHeaderItems = tableHeaderItems;
    }

    @Override
    public void init(InstanceInfo instanceInfo, Map map) {
        //监控服务器Id
        if (instanceInfo.getData() != null && intValueConvert(instanceInfo.getData().get(MonitorServerIdKey)) > 0) {
            viewProcessorManage.addFieldIdSet(RelatedDataType.monitorServer.name(), strValueConvert(instanceInfo.getData().get(MonitorServerIdKey)));
            map.put(monitorServerNameKey, strValueConvert(instanceInfo.getData().get(MonitorServerIdKey)));
        }
        //轮询引擎id
        if (instanceInfo.getData() != null && !Strings.isNullOrEmpty(strValueConvert(instanceInfo.getData().get(PollingEngineKey)))) {
            viewProcessorManage.addFieldIdSet(RelatedDataType.pollingEngine.name(), strValueConvert(instanceInfo.getData().get(PollingEngineKey)));
            //轮询引擎类型为2(引擎分组)
            if (intValueConvert(instanceInfo.getData().get(ProxyTypeKey)) == 2) {
                map.put(ProxyTypeKey, intValueConvert(instanceInfo.getData().get(ProxyTypeKey)));
                viewProcessorManage.addFieldIdSet(proxyGroupKey, strValueConvert(instanceInfo.getData().get(PollingEngineKey)));
            }
            map.put(pollingEngineNameKey, strValueConvert(instanceInfo.getData().get(PollingEngineKey)));
        }
    }

    @Override
    public void selectRelateData(TableDataProcessorContext context) {
        MwTPServerService mwTPServerService = (MwTPServerService) context.get(ViewConvertContext.MwTPServerServiceKey);
        MwEngineManageService mwEngineManageService = (MwEngineManageService) context.get(ViewConvertContext.MwEngineManageServiceKey);
        Set<String> monitorServerIdStrs = viewProcessorManage.getFieldIdSet(RelatedDataType.monitorServer.name());
        if (null != monitorServerIdStrs) {
            List<Integer> monitorServerIds = monitorServerIdStrs.stream().mapToInt(data -> intValueConvert(data)).boxed().collect(Collectors.toList());
            List<MwTPServerTable> mwTPServerByServiceIds = mwTPServerService.getMwTPServerByServiceIds(monitorServerIds);
            if (null != mwTPServerByServiceIds) {
                for (MwTPServerTable mwDto : mwTPServerByServiceIds) {
                    viewProcessorManage.addFieldIdMap(RelatedDataType.monitorServer.name(), strValueConvert(mwDto.getId()), mwDto.getMonitoringServerName());
                }
            }
        }

        Set<String> pollingEngineIdStrs = viewProcessorManage.getFieldIdSet(RelatedDataType.pollingEngine.name());
        Set<String> proxyGroupIdStrs = viewProcessorManage.getFieldIdSet(proxyGroupKey);
        if (null != pollingEngineIdStrs) {
            List<MwEngineManageTableDto> engineManageList = mwEngineManageService.getEngineManageByEngineIds(new ArrayList<>(pollingEngineIdStrs));
            if (null != engineManageList) {
                for (MwEngineManageTableDto mwDto : engineManageList) {
                    viewProcessorManage.addFieldIdMap(RelatedDataType.pollingEngine.name(), mwDto.getId(), mwDto.getEngineName());
                }
            }
        }
        if (null != proxyGroupIdStrs) {
            List<MwEngineProxyGroupInfo> proxyGroupInfoList = mwEngineManageService.getEngineProxyGroupInfoByIds(new ArrayList<>(proxyGroupIdStrs));
            if (null != proxyGroupInfoList) {
                for (MwEngineProxyGroupInfo mwDto : proxyGroupInfoList) {
                    viewProcessorManage.addFieldIdMap(proxyGroupKey, strValueConvert(mwDto.getId()), mwDto.getName());
                }
            }
        }


    }

    @Override
    public void setRelateData(Map map) {
        //监控服务器名称
        Map<String, String> fieldIdMap = viewProcessorManage.getFieldIdMap(RelatedDataType.monitorServer.name());
        if(fieldIdMap!=null ){
            String monitorServerName = fieldIdMap.get(strValueConvert(map.get(monitorServerNameKey)));
            map.put(monitorServerNameKey, monitorServerName);

            //轮询引擎名称
            String pollingEngineName = "";
            String pollingEngineId = strValueConvert(map.get(pollingEngineNameKey));
            if (!Strings.isNullOrEmpty(monitorServerName)) {
                if (intValueConvert(map.get(ProxyTypeKey)) == 2) {
                    Map<String, String> proxyTypeGroupMap = viewProcessorManage.getFieldIdMap(proxyGroupKey);
                    if (proxyTypeGroupMap!=null){
                        pollingEngineName = proxyTypeGroupMap.get(pollingEngineId);
                    }
                } else {
                    Map<String, String> pollingEngineMap = viewProcessorManage.getFieldIdMap(RelatedDataType.pollingEngine.name());
                    if(pollingEngineMap!=null){
                        if (Strings.isNullOrEmpty(pollingEngineId)) {
                            pollingEngineName = "本机";
                        } else {
                            pollingEngineName = pollingEngineMap.get(pollingEngineId);
                        }
                    }
                }
            }
            map.put(pollingEngineNameKey,pollingEngineName);
        }
    }



    @Override
    public int hashCode() {
        return getClass().getSimpleName().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return getClass().getSimpleName().equals(obj.getClass().getSimpleName());
    }
}
